package day414

import (
	"math"
)

func myAtoi(s string) (ans int) {
	n := len(s)
	sign := 1
	i := 0
	for i < n && s[i] == ' ' {
		i++
	}
	if i < n {
		if s[i] == '+' {
			i++
		} else if s[i] == '-' {
			sign = -1
			i++
		}
	}
	for i < n && s[i] >= '0' && s[i] <= '9' {
		ans = ans*10 + int(s[i]-'0')
		if sign*ans < math.MinInt32 {
			return math.MinInt32
		} else if sign*ans > math.MaxInt32 {
			return math.MaxInt32
		}
		i++
	}
	return ans * sign
}
